import { useCallback, useRef } from "react";
import Taro, { useDidShow } from "@tarojs/taro";
var TIMEOUT_INTERVAL = 5000; // 跳转超时时间
var resolveCallback = null;
var useNavigation = function (props) {
    var dbClickRef = useRef(false); //防双击操作
    var passTimeRef = useRef(0);
    useDidShow(function () {
        dbClickRef.current = false;
    });
    var formateParams = function (o) {
        if (!o) {
            return "";
        }
        var params = "";
        for (var k in o) {
            if (typeof o[k] === "object") {
                //参数有对象时，先转为字符串，后url编码
                //页面获取对象参数时，先url解码，后解析为对象
                params += "&" + k + "=" + encodeURI(JSON.stringify(o[k]));
            }
            else {
                params += "&" + k + "=" + o[k];
            }
        }
        params = params.replace(/&/, "?");
        return params;
    };
    var push = useCallback(function (path, o) {
        var nowTime = new Date().getTime();
        if (dbClickRef.current) {
            // 与上次操作间隔时间
            var interval = nowTime - passTimeRef.current;
            if (interval < TIMEOUT_INTERVAL) {
                // 未超过超时时间，则丢弃
                return Promise.reject({ code: 1000, errMsg: "操作过于频繁!" });
            }
        }
        passTimeRef.current = nowTime;
        dbClickRef.current = true;
        var pages = {};
        var addr = path.indexOf("/") === -1 ? pages[path] : path;
        return new Promise(function (resolve, reject) {
            var url = addr + formateParams(o);
            Taro.navigateTo({
                url: url,
                success: function () {
                    resolveCallback = resolve;
                },
                fail: function (res) {
                    dbClickRef.current = false;
                    reject(res);
                }
            });
        });
    }, []);
    var redirectTo = useCallback(function (path, o) {
        // 先清空上一次存储的resolve, 避免返回的时候, 触发了遗留的resolve
        resolveCallback = null;
        var pages = {};
        var addr = path.indexOf("/") === -1 ? pages[path] : path;
        return new Promise(function (resolve, reject) {
            var url = addr + formateParams(o);
            Taro.redirectTo({
                url: url,
                success: resolve,
                fail: reject
            });
        });
    }, []);
    var pop = useCallback(function (delta, params) {
        if (Taro.getCurrentPages().length < 2) {
            return new Promise(function (resolve, reject) {
                Taro.reLaunch({
                    url: props && props.popPath ? props.popPath : "/pages/index/index",
                    success: resolve,
                    fail: reject
                });
            });
        }
        return new Promise(function (resolve, reject) {
            var del = delta ? delta : 1;
            Taro.navigateBack({
                delta: del,
                success: function () {
                    /**
                     *  只有返回一页的时候才会触发,连续返回几页则不触发
                     */
                    if (del === 1) {
                        resolveCallback === null || resolveCallback === void 0 ? void 0 : resolveCallback(params);
                    }
                    resolveCallback = null;
                    resolve(null);
                },
                fail: reject
            });
        });
    }, []);
    return { push: push, pop: pop, redirectTo: redirectTo, switchTab: Taro.switchTab, reLunch: Taro.reLaunch };
};
export default useNavigation;
